/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.kamus.iwic.main;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.SplashScreen;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author mamat
 */
public class MKamus extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    public String cariKata = null;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command cmdSearch;
    private Command cmdMenuPencarian;
    private Command cmdExit1;
    private Command cmdExit;
    private SimpleCancellableTask task;
    private Image image1;
    private Image iFound;
    private Image iOther;
    private Image iNFoudn;
    private WaitScreen waitScreen;
    private Form fHasilPencarian;
    private Form fPencarian;
    private TextField tfKata;
    private SplashScreen splashScreen;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The MKamus constructor.
     */
    public MKamus() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fPencarian ">//GEN-BEGIN:|13-getter|0|13-preInit
    /**
     * Returns an initiliazed instance of fPencarian component.
     * @return the initialized component instance
     */
    public Form getFPencarian() {
        if (fPencarian == null) {//GEN-END:|13-getter|0|13-preInit
            // write pre-init user code here
            fPencarian = new Form("PECARIAN KATA - English to Indonesia", new Item[] { getTfKata() });//GEN-BEGIN:|13-getter|1|13-postInit
            fPencarian.addCommand(getCmdSearch());
            fPencarian.addCommand(getCmdExit());
            fPencarian.setCommandListener(this);//GEN-END:|13-getter|1|13-postInit
            // write post-init user code here
        }//GEN-BEGIN:|13-getter|2|
        return fPencarian;
    }
    //</editor-fold>//GEN-END:|13-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfKata ">//GEN-BEGIN:|15-getter|0|15-preInit
    /**
     * Returns an initiliazed instance of tfKata component.
     * @return the initialized component instance
     */
    public TextField getTfKata() {
        if (tfKata == null) {//GEN-END:|15-getter|0|15-preInit
            // write pre-init user code here
            tfKata = new TextField("Masukkan Kata", "", 32, TextField.ANY);//GEN-LINE:|15-getter|1|15-postInit
            // write post-init user code here
        }//GEN-BEGIN:|15-getter|2|
        return tfKata;
    }
    //</editor-fold>//GEN-END:|15-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSearch ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of cmdSearch component.
     * @return the initialized component instance
     */
    public Command getCmdSearch() {
        if (cmdSearch == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            cmdSearch = new Command("Cari", Command.OK, 0);//GEN-LINE:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return cmdSearch;
    }
    //</editor-fold>//GEN-END:|16-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdExit ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of cmdExit component.
     * @return the initialized component instance
     */
    public Command getCmdExit() {
        if (cmdExit == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            cmdExit = new Command("Keluar", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return cmdExit;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdMenuPencarian ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of cmdMenuPencarian component.
     * @return the initialized component instance
     */
    public Command getCmdMenuPencarian() {
        if (cmdMenuPencarian == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            cmdMenuPencarian = new Command("Cari Kata Lagi", "Pencarian Kata lagi", Command.OK, 0);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return cmdMenuPencarian;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdExit1 ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of cmdExit1 component.
     * @return the initialized component instance
     */
    public Command getCmdExit1() {
        if (cmdExit1 == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            cmdExit1 = new Command("Keluar", Command.EXIT, 0);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return cmdExit1;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == fHasilPencarian) {//GEN-BEGIN:|7-commandAction|1|25-preAction
            if (command == cmdExit1) {//GEN-END:|7-commandAction|1|25-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|2|25-postAction
                // write post-action user code here
            } else if (command == cmdMenuPencarian) {//GEN-LINE:|7-commandAction|3|23-preAction
                // write pre-action user code here
                switchDisplayable(null, getFPencarian());//GEN-LINE:|7-commandAction|4|23-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|19-preAction
        } else if (displayable == fPencarian) {
            if (command == cmdExit) {//GEN-END:|7-commandAction|5|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|19-postAction
                // write post-action user code here
            } else if (command == cmdSearch) {//GEN-LINE:|7-commandAction|7|17-preAction
                // write pre-action user code here
                switchDisplayable(null, getWaitScreen());//GEN-LINE:|7-commandAction|8|17-postAction
                // write post-action user code here
                
                new Thread(new Runnable() {

                    public void run() {
                        cariKata=tfKata.getString();
                        KamusKSoap mKamusSoap;
                        String xml = null;
                        Vector v = new Vector();
                        
                        try {
                            //throw new UnsupportedOperationException("Not supported yet.");
                            mKamusSoap = new KamusKSoap();
                            KamusParser kp = new KamusParser();
                            xml = mKamusSoap.getKamusWs(cariKata);
                            try{
                                v = kp.parserXMlKamus(xml);                                
                            } catch (Exception e){
                                e.printStackTrace();
                                System.out.println("Errmmm");
                            }
                         } catch (IOException ex) {
                            ex.printStackTrace();   
                        }
                        fHasilPencarian.deleteAll();
                        //fHasilPencarian.append(new StringItem("Hasil Pencarian",xml));
                        boolean icek = false;
                      
                        for(int i=0;i<v.size();i++){
                            Kamus k = (Kamus)v.elementAt(i);
                            if(k.getId().equals("0")){
                                fHasilPencarian.append(new ImageItem("KATA TIDAK DITEMUKAN", getINFoudn(),ImageItem.LAYOUT_DEFAULT,""));
                                fHasilPencarian.append(new StringItem("'"+cariKata+"'","Wew MAAF..!, silahkan kirim KATA yang anda cari ke Email(mamat@amikom.ac.id) kami akan melengkapi databasenya :), TRIMA KASIH"));
                            } else {
                                if(i==0){
                                    fHasilPencarian.append(new ImageItem("KATA DITEMUKAN", getIFound(),ImageItem.LAYOUT_DEFAULT,""));
                                } else if(icek == false) {
                                    fHasilPencarian.append(new ImageItem("KATA YANG DITEMUKAN LAINNYA", getIOther(),ImageItem.LAYOUT_DEFAULT, ""));
                                    icek=true;
                                }
                                fHasilPencarian.append(new StringItem(""+k.getKata(), ""+k.getDeskripsi()));
                            }                            
                        }
                    }
                }).start();
            }//GEN-BEGIN:|7-commandAction|9|47-preAction
        } else if (displayable == splashScreen) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|9|47-preAction
                // write pre-action user code here
                switchDisplayable(null, getFPencarian());//GEN-LINE:|7-commandAction|10|47-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|34-preAction
        } else if (displayable == waitScreen) {
            if (command == WaitScreen.FAILURE_COMMAND) {//GEN-END:|7-commandAction|11|34-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|12|34-postAction
                // write post-action user code here
            } else if (command == WaitScreen.SUCCESS_COMMAND) {//GEN-LINE:|7-commandAction|13|33-preAction
                // write pre-action user code here
                switchDisplayable(null, getFHasilPencarian());//GEN-LINE:|7-commandAction|14|33-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|7-postCommandAction
        }//GEN-END:|7-commandAction|15|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|16|
    //</editor-fold>//GEN-END:|7-commandAction|16|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fHasilPencarian ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of fHasilPencarian component.
     * @return the initialized component instance
     */
    public Form getFHasilPencarian() {
        if (fHasilPencarian == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            fHasilPencarian = new Form("HASIL PENCARIAN", new Item[] { });//GEN-BEGIN:|21-getter|1|21-postInit
            fHasilPencarian.addCommand(getCmdMenuPencarian());
            fHasilPencarian.addCommand(getCmdExit1());
            fHasilPencarian.setCommandListener(this);//GEN-END:|21-getter|1|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return fHasilPencarian;
    }
    //</editor-fold>//GEN-END:|21-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of task component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getTask() {
        if (task == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            task = new SimpleCancellableTask();//GEN-BEGIN:|35-getter|1|35-execute
            task.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {//GEN-END:|35-getter|1|35-execute
                    // write task-execution user code here
                }//GEN-BEGIN:|35-getter|2|35-postInit
            });//GEN-END:|35-getter|2|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|3|
        return task;
    }
    //</editor-fold>//GEN-END:|35-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|37-getter|1|37-@java.io.IOException
                image1 = Image.createImage("/net/kamus/iwic/icon/detective124.png");
            } catch (java.io.IOException e) {//GEN-END:|37-getter|1|37-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|37-getter|2|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|37-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: waitScreen ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of waitScreen component.
     * @return the initialized component instance
     */
    public WaitScreen getWaitScreen() {
        if (waitScreen == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            waitScreen = new WaitScreen(getDisplay());//GEN-BEGIN:|30-getter|1|30-postInit
            waitScreen.setTitle("PENCARIAN");
            waitScreen.setCommandListener(this);
            waitScreen.setImage(getImage1());
            waitScreen.setText("Sedang melakukan pencarian.....");
            waitScreen.setTask(getTask());//GEN-END:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return waitScreen;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: iFound ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of iFound component.
     * @return the initialized component instance
     */
    public Image getIFound() {
        if (iFound == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|39-getter|1|39-@java.io.IOException
                iFound = Image.createImage("/net/kamus/iwic/icon/happy32.png");
            } catch (java.io.IOException e) {//GEN-END:|39-getter|1|39-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|39-getter|2|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|3|
        return iFound;
    }
    //</editor-fold>//GEN-END:|39-getter|3|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: iOther ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of iOther component.
     * @return the initialized component instance
     */
    public Image getIOther() {
        if (iOther == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|40-getter|1|40-@java.io.IOException
                iOther = Image.createImage("/net/kamus/iwic/icon/dumbfounded32.png");
            } catch (java.io.IOException e) {//GEN-END:|40-getter|1|40-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|40-getter|2|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|3|
        return iOther;
    }
    //</editor-fold>//GEN-END:|40-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: iNFoudn ">//GEN-BEGIN:|44-getter|0|44-preInit
    /**
     * Returns an initiliazed instance of iNFoudn component.
     * @return the initialized component instance
     */
    public Image getINFoudn() {
        if (iNFoudn == null) {//GEN-END:|44-getter|0|44-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|44-getter|1|44-@java.io.IOException
                iNFoudn = Image.createImage("/net/kamus/iwic/icon/hmpf32.png");
            } catch (java.io.IOException e) {//GEN-END:|44-getter|1|44-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|44-getter|2|44-postInit
            // write post-init user code here
        }//GEN-BEGIN:|44-getter|3|
        return iNFoudn;
    }
    //</editor-fold>//GEN-END:|44-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|45-getter|0|45-preInit
    /**
     * Returns an initiliazed instance of splashScreen component.
     * @return the initialized component instance
     */
    public SplashScreen getSplashScreen() {
        if (splashScreen == null) {//GEN-END:|45-getter|0|45-preInit
            // write pre-init user code here
            splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|45-getter|1|45-postInit
            splashScreen.setTitle("MOBILE KAMUS");
            splashScreen.setCommandListener(this);
            splashScreen.setImage(getImage1());
            splashScreen.setText("Supported by STMIK AMIKOM Yogyakarta");//GEN-END:|45-getter|1|45-postInit
            // write post-init user code here
        }//GEN-BEGIN:|45-getter|2|
        return splashScreen;
    }
    //</editor-fold>//GEN-END:|45-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
